package com.supermarket.controller;

import com.supermarket.common.ResponseResult;
import com.supermarket.entity.ArticleLike;
import com.supermarket.exception.ArticleException;
import com.supermarket.service.ArticleLikeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article-like")
public class ArticleLikeController {

    @Resource
    private ArticleLikeService articleLikeService;

    /**
     * 点赞文章
     *
     * @param articleId 文章ID
     * @param userId    用户ID
     * @return 包含点赞结果的响应结果
     */
    @PostMapping("/{articleId}")
    public ResponseResult<Map<String, Object>> likeArticle(
            @PathVariable Integer articleId,
            @RequestParam Integer userId) {

        if (userId == null){
            return ResponseResult.error(400, "用户ID不能为空");
        }

        try {
            int likeCount = articleLikeService.likeArticle(articleId, userId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("liked", true);
            result.put("likeCount", likeCount);
            
            return ResponseResult.success(result);
        } catch (ArticleException e) {
            return ResponseResult.error(400, e.getMessage());
        }
    }

    /**
     * 取消点赞
     *
     * @param articleId 文章ID
     * @param userId    用户ID
     * @return 包含取消点赞结果的响应结果
     */
    @DeleteMapping("/{articleId}")
    public ResponseResult<Map<String, Object>> unlikeArticle(
            @PathVariable Integer articleId,
            @RequestParam Integer userId) {

        if (userId == null){
            return ResponseResult.error(400, "用户ID不能为空");
        }

        try {
            int likeCount = articleLikeService.unlikeArticle(articleId, userId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("liked", false);
            result.put("likeCount", likeCount);
            
            return ResponseResult.success(result);
        } catch (ArticleException e) {
            return ResponseResult.error(400, e.getMessage());
        }
    }

    /**
     * 检查用户是否点赞过文章
     *
     * @param articleId 文章ID
     * @param userId    用户ID
     * @return 包含点赞状态的响应结果
     */
    @GetMapping("/check/{articleId}")
    public ResponseResult<Boolean> checkUserLiked(
            @PathVariable Integer articleId,
            @RequestParam Integer userId) {
        
        boolean hasLiked = articleLikeService.hasUserLikedArticle(articleId, userId);
        return ResponseResult.success(hasLiked);
    }

    /**
     * 获取用户点赞的所有文章
     *
     * @param userId 用户ID
     * @return 包含点赞文章列表的响应结果
     */
    @GetMapping("/user/{userId}")
    public ResponseResult<List<ArticleLike>> getUserLikedArticles(@PathVariable Integer userId) {
        List<ArticleLike> likedArticles = articleLikeService.getUserLikedArticles(userId);
        return ResponseResult.success(likedArticles);
    }

    /**
     * 批量检查用户是否点赞过文章
     *
     * @param userId     用户ID
     * @param articleIds 文章ID列表
     * @return 包含用户点赞过的文章ID列表的响应结果
     */
    @PostMapping("/batch-check")
    public ResponseResult<List<Integer>> batchCheckUserLiked(
            @RequestParam Integer userId,
            @RequestBody List<Integer> articleIds) {
        
        List<Integer> likedArticleIds = articleLikeService.getUserLikedArticleIds(userId, articleIds);
        return ResponseResult.success(likedArticleIds);
    }

    /**
     * 获取文章点赞数
     *
     * @param articleId 文章ID
     * @return 包含点赞数的响应结果
     */
    @GetMapping("/count/{articleId}")
    public ResponseResult<Integer> getArticleLikeCount(@PathVariable Integer articleId) {
        int likeCount = articleLikeService.getArticleLikeCount(articleId);
        return ResponseResult.success(likeCount);
    }
} 